import requests
# 拿到页面源代码
import re
import csv

url = "https://movie.douban.com/top250"
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}
resp = requests.get(url, headers=headers)
page_content = resp.text
# 页面源代码名字page_content

# 用正则表达式抓取电影名字
obj = re.compile(r'<li>.*?<div class="item">.*? <span class="title">(?P<name>.*?)</span>', re.S)
# 开始匹配
result = obj.finditer(page_content)
# f = open("data.csv", mode="w")
# csvwriter = csv.writer(f)
for it in result:
    print(it.group("name"))
   # 用字典
  # dic = it.groupdict()
  # csvwriter.writerow(dic.values())



print("over!")
